package com.app.mymusic.ui.greendao;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.app.mymusic.R;
import com.app.mymusic.utils.LogUtil;
import com.example.chenlijin.greendao.DaoMaster;
import com.example.chenlijin.greendao.DaoSession;
import com.example.chenlijin.greendao.Message;
import com.example.chenlijin.greendao.MessageDao;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GreendaoActivity extends AppCompatActivity {

    @BindView(R.id.t1)
    TextView t1;
    @BindView(R.id.t2)
    TextView t2;
    @BindView(R.id.t3)
    TextView t3;
    @BindView(R.id.t4)
    TextView t4;
    @BindView(R.id.t5)
    TextView t5;
    @BindView(R.id.t6)
    TextView t6;
    @BindView(R.id.t7)
    TextView t7;
    @BindView(R.id.t8)
    TextView t8;
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private MessageDao messageDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_greendao);
        ButterKnife.bind(this);
        initDb();
    }

    private void initDb() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "msg_db", null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        messageDao = daoSession.getMessageDao();

    }

    @OnClick({R.id.t1, R.id.t2, R.id.t3, R.id.t4, R.id.t5, R.id.t6, R.id.t7, R.id.t8})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.t1:
                Message msg=new Message(2454L,"haha","you ge shi qing",new Date());
                Message msg2=new Message(2454L,"haha2","you ge shi qing2",new Date());
                messageDao.insert(msg);
                messageDao.insertInTx(msg,msg2);
                break;
            case R.id.t2:
                List<Message> list= messageDao.queryBuilder().list();
                messageDao.queryBuilder().where(MessageDao.Properties.Text.eq("haha"),MessageDao.Properties.Text.eq("haha")).build();
                if(!list.isEmpty()){

                    LogUtil.showLog("list:"+list.get(0).getComment());
                }
                break;
            case R.id.t3:
                Message msg1=new Message(2454L,"haha","you ge shi qing3",new Date());

                messageDao.update(msg1);
                break;
            case R.id.t4:
                messageDao.deleteAll();
                break;
            case R.id.t5:
                break;
            case R.id.t6:
                break;
            case R.id.t7:
                break;
            case R.id.t8:
                break;
        }
    }
}
